<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>美滴滴</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link type="text/css" rel="stylesheet" media="screen" href="__FONT__/css/style.css"/>
    <link type="text/css" rel="stylesheet" media="screen" href="__FONT__/css/media.css"/>
    <script type="text/javascript" src="__FONT__/js/jquery-1.9.1.min.js"></script>
</head>
<body style="background:#fff;">
	<form action="{:U('/User/bind')}" method="post" id="bindForm">
		<input type="hidden" name="redirect" value="{$redirect}">
 	<section>
      <div class="login">
      	<h1><img src="__FONT__/images/loginlogo.png"></h1>
        <ul>
          <li><input type="tel" name="phone" id="phone" placeholder="请输入会员手机号码" class="phone" ></li>
          <li>
              <input type="tel" name="code" id="code" placeholder="输入您获取到的验证码" class="code">
          </li>
          <li>
              <input type="button" class="submit" id="huoqu" value="获取验证码">
              <div  class="time" style="display:none;"><span>60</span>s重获</div>
          </li>
          <li><input type="button" value="立即登陆" class="dllogin" ></li>  <!--填写完addClass("addbg")-->
        </ul>
      </div> 
	</section>
	</form>
<script>
function abc() {
    $('#huoqu').hide();
    $('.time').show();
    var sec = $(".time span").text();
    $(".time span").text(--sec);
    if (sec >= 1) {
        setTimeout("abc();", 1000);
    } else {
        $('#huoqu').show();
        $('.time').hide();
        $(".time span").text(60);
    }
    return false;
}

var getCode = false, sendCode = false;
function getValidCode() {
	$.ajax({
		url: '{:U("/User/sendCode")}',
		data: {phone: $('input[name="phone"]').val()},
		dataType: 'json',
		type: 'GET',
		success: function (result) {
			getCode = false;
			if (result.status) {
				sendCode = true;
				abc();
			} else {
				sendCode = false;
				alert('获取验证码失败！请重试');
			}
		},
		error: function () {
			getCode = false;
			sendCode = false;
			alert('获取验证码失败！请重试');
		}
	});
}

var phoneExistsCache = {};

function checkPhoneExists(phone) {
	return typeof phoneExistsCache [phone] == 'undefined'?'undefined':phoneExistsCache [phone];
}

$(function () {
	var valid=false;
	$("#phone,#code").keyup(function () {
		var phone = $('input[name="phone"]').val();
		var reg=/^0?1[3|4|5|7|8][0-9]\d{8}$/;
		
		if ($.trim(phone) == '' || !reg.test(phone)) {
			$('.dllogin,#huoqu').removeClass('addbg');
			return false;
		}
		
		$('#huoqu').addClass('addbg');
		
		var code = $('input[name="code"]').val();
		var reg=/^\d{6}$/;
		if (!reg.test(code)) {
			$('.dllogin').removeClass('addbg');
			return false;
		}
		
		$('.dllogin').addClass('addbg');
	});
	
	var submitUserInfo;
	$(".dllogin").click(function () {
		if ( !sendCode ) {
			alert("请先获取验证码");
			return false;
		}
		
		if (!valid) {
			alert("手机号已绑定其它帐号");
			return false;
		}
		
		if (submitUserInfo && submitUserInfo.state() == 'pending') {
			return false;
		}
		
		var phone = $('input[name="phone"]').val();
		var reg=/^\d{11}$/;
		if (!reg.test(phone)) {
			alert('输入的手机号码不正确');
			return false;
		}
		
		var code = $('input[name="code"]').val();
		var reg=/^\d{6}$/;
		if (!reg.test(code)) {
			alert('输入的验证码格式不正确');
			return false;
		}
		
		$('form').submit();
		return true;
	});
	
	$("#huoqu").click(function () {
		if (getCode) {
			alert('正在获取，请等待');
		}
		
		var phone = $('input[name="phone"]').val();
		if ($.trim(phone) == '') {
			$('.dllogin,#huoqu').removeClass('addbg');
			alert('请输入手机号码');
			return false;
		}
		
		var reg=/^0?1[3|4|5|7|8][0-9]\d{8}$/;
		if (!reg.test(phone)) {
			$('.dllogin,#huoqu').removeClass('addbg');
			alert('输入的手机号码不正确');
			return false;
		}
		
		if (typeof phoneExistsCache [phone] == 'undefined') {
			$.ajax({
				url: '{:U("/User/phoneHasBind")}',
				data: {'phone': phone},
				type: 'get',
				dataType: 'json',
				success: function(msg) {
					if(msg.status == 0) {  // 手机号已注册
						phoneExistsCache [phone] = true;
						valid = false;
						alert("手机号已绑定其它账户");
					} else {
						phoneExistsCache [phone] = false;
						getCode =true;
						valid = true;
						getValidCode();
					}
				}
			});
		} else {
			if (!phoneExistsCache [phone]) {
				getCode =true;
				valid = true;
				getValidCode();
			} else {
				valid = false;
				alert("手机号已绑定其它账户");
			}
		}
	});
});
</script>
</body>
</html>